package cn.teud.straw.gateway.servive.impl;


import cn.tedu.straw.commons.model.*;
import cn.teud.straw.gateway.mapper.PermissionMapper;
import cn.teud.straw.gateway.mapper.UserMapper;
import cn.teud.straw.gateway.servive.IUserService;
import cn.teud.straw.gateway.vo.UserLoginVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-08-11
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public UserLoginVO getUserLoginDetails(String username) {
        //基于参数username调用userMapper.findByUsername()方法查询用户信息
        User user = userMapper.findByUsername(username);
        //判断查询结果是否为null
        if (user == null) {
            //返回null表示"用户不存在"
            return null;
        }
        //基于参数username调用permissionMapper.findByUsername()方法查询该用户的权限列表
        List<Permission> permissions = permissionMapper.findByUsername(username);
        //创建UserLoginVO对象，并封装个属性的值
        UserLoginVO userLoginVO = new UserLoginVO();

        userLoginVO.setId(user.getId());
        userLoginVO.setNickname(user.getNickname());
        userLoginVO.setUsername(user.getUsername());
        userLoginVO.setPassword(user.getPassword());
        userLoginVO.setPhone(user.getPhone());
        userLoginVO.setType(user.getType());
        userLoginVO.setGender(user.getGender());
        userLoginVO.setLocked(user.getLocked());
        userLoginVO.setEnabled(user.getEnabled());
        userLoginVO.setPermissions(permissions);
        //返回UserLoginVO对象
        return userLoginVO;
    }

}
